<?php
namespace app\admin\model;

use think\Model;
use think\Db;
use think\facade\Config;
class Class extends Model
{   

	/*查询数据 */
	public function get_data(){
		$req=request()->param();
        if(!empty($req['start_time'])||!empty($req['end_time'])||!empty($req['title'])||!empty($req['author'])){
        	if(!empty($req['start_time'])){
        		$sear_where[]=['create_time','>=',$req['start_time']." 00:00:00"];
        	}
        	if(!empty($req['end_time'])){
        		$sear_where[]=['create_time','<',$req['end_time']." 23:59:59"];
        	}
        	if(!empty($req['title'])){
        		$sear_where[]=['title','like',"%".$req['title']."%"];
        	}
        	if(!empty($req['author'])){
        		$sear_where[]=['author','like',"%".$req['author']."%"];
        	}
			$list=Blog::where($sear_where)
			->where([['status','=',1]])
            ->order('create_time','desc')
            ->paginate(Config::get('app.admin_page_number'),false,['query' => $req]);
        }else{
            $list=Blog::where([['status','=',1]])->order('create_time','desc')
              ->paginate(Config::get('app.admin_page_number'),false,['query' => $req]);
        }
        $data['list']=$list;
        if(empty($req['start_time'])){$data['start_time']='';}else{$data['start_time']=$req['start_time'];}
        if(empty($req['end_time'])){$data['end_time']='';}else{$data['end_time']=$req['end_time'];}
        if(empty($req['title'])){$data['title']='';}else{$data['title']=$req['title'];}
        if(empty($req['author'])){$data['author']='';}else{$data['author']=$req['author'];}
        return $data;
	}
	/*获取一条数据用于修改*/
	public function get_edit($id){
        $data=Blog::where([['status','=',1],['id','=',$id]])->find();
        return $data;  
	}
	/*新增*/
	public function insert_data($title,$author,$newname,$content){
        $Blog = new Blog;
		$result=$Blog->save([
		    'title'  =>  trim($title),
		    'author' =>  trim($author),
		    'content' =>  trim($content),
		    'img' =>  trim($newname),
		    'create_time' => date("Y-m-d H:i:s",time())
		]);
		return $result;
	}
	/*修改*/
	public function update_data($id,$title,$author,$newname,$content){
        $data = Blog::get($id);
        $data->title = $title;
        $data->img = $newname;
        $data->author = $author;
        $data->content = $content;
        $result=$data->save();
		return $result;    
	}
	/*删除假动作实际为修改操作*/
	public function delete_data($id){
        $data = Blog::get($id);
        if($data['status']!=1){
        	$result=['status'=>'0','msg'=>'无需操作'];
	        return json($result);
        }
		$data->status = '2';
		$result=$data->save();
		return $result;
	}
}
 